package rtda

import (
	"gitee.com/thghh/jvmgo/rtda/heap"
)

/**
 *  实现线程私有的运行时数据区
 *  pc(program count) pc寄存器
 *  stack Java虚拟机栈
 *  结构示例：
 *     Thread
 *          |- pc
 *          |- JVM Stack
 *                     |-Frame
 *                           |- Local Variable
 *                           |- Operand Stack
 */
type Thread struct {
	pc    int // the address of the instruction currently being executed
	stack *Stack
	// todo
}

func NewThread() *Thread {
	return &Thread{
		stack: newStack(1024),
	}
}

/**
 * 当前正在执行的Java虚拟机指令的地址,如果只为0当前方法是本地方法
 */
func (self *Thread) GetPC() int {
	return self.pc

}

func (self *Thread) SetPC(pc int) {
	self.pc = pc
}

func (self *Thread) PushFrame(frame *Frame) {
	self.stack.push(frame)
}

func (self *Thread) PopFrame() *Frame {
	return self.stack.pop()
}

/**
 * 返回线程的当前方法的当前帧
 */
func (self *Thread) CurrentFrame() *Frame {
	return self.stack.top()
}

func (self *Thread) TopFrame() *Frame {
	return self.stack.top()
}

func (self *Thread) IsStackEmpty() bool {
	return self.stack.isEmpty()
}

func (self *Thread) NewFrame(method *heap.Method) *Frame {
	return newFrame(self, method)
}

func (self *Thread) ClearStack() {
	self.stack.clear()
}

// 获取完整的虚拟机栈
func (self *Thread) GetFrames() []*Frame {
	return self.stack.getFrames()
}
